﻿@model IEnumerable<Common.Product>
           
@{
    ViewBag.Title = "Electros Ltd | Products";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>View Products</h2>
<div style="color:Red">
    @ViewBag.Message
</div>
<script type="text/javascript">
    
    function addtocart(id, prodID) {
        var qty = $("#quantity" + id + "").val();
        var validate = new RegExp('^[0-9]$', "");

        //Validating that textbox is not left empty
        if (qty == "") {
            alert("Input quantity to buy!");
        }
        else if ((qty == "0") || (parseInt(qty) < 0)) {
            alert("Quantity should be more than 0!");
        }
        //Validating that only digits are entered in the textbox
        else if (isNaN(qty)) {
            alert("Only digits are accepted");
        }
        else if (!validate.test(qty)) {
            alert("Invalid quantity");
        }
        else {
            var param = {
                pId: prodID,
                quantity: qty
            };
            $.ajax({
                url: '@Url.Action("AddToCart")',
                type: "POST",
                datatype: "json",
                data: param,
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert(xmlHttpRequest + "|" + errorText + "|" + thrownError);
                },
                success: function (data) {
                    var msg = data.hasOwnProperty("not enough stock") ? data.d : data;
                    if (data != null) {
                        if (data == "not enough stock") {
                            alert("Not enough product stock! Try a lower quantity");
                            $('#quantity' + id + '').val("");
                        }
                        else {
                            alert("Product has been successfully added to your cart!");
                            $('#quantity' + id + '').val("");
                        }
                    }
                }
            });
        }
    }
</script>

@if (Model.Count() > 0)
{
    <table>
        <tr>
            <th>Product Name</th>
            <th>Features</th>
            <th>Image</th>
            <th>Stock</th>
            <th>Price</th>
            <th>Date Listed</th>
            <th>Average Rating</th>
            <th>Comments</th>
            <th>View Comments</th>
            <th>Add to Cart</th>
        </tr>
    @{
        int counter = 1;
    }

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Features)
            </td>
            <td>
                <img height="150px" width="150px" src= "@Url.Content(item.Image)" alt="@Html.DisplayFor(modelItem => item.Image)" />
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stock)
            </td>
            <td>
                €@Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DateListed)
            </td>
            <td>
                @Html.Action("GetAverageRatingForProduct", "Products", new { productID = item.ProductID })
            </td>
            <td>
                <div id='@counter'></div>
            </td>
            <td>
                @Ajax.ActionLink("View Comments", "GetCommentsForProduct", new { productID = item.ProductID },
                    new AjaxOptions
                    {
                        HttpMethod = "GET",
                        UpdateTargetId = counter.ToString(),
                        InsertionMode = InsertionMode.Replace
                    })
            </td>
            <td>
                @if (item.Stock != 0)
                {
                    @Html.Label("Quantity:") <br />
                    @Html.TextBox("quantity" + counter + "")
                  
                    <button type="button" onclick="addtocart(@counter, @item.ProductID)">Add to Cart</button>
                }
                else
                {
                    <span style="color:Red">
                        @Html.Label("Out of stock!")
                    </span>
                } 
            </td>
        </tr>
        { counter++; }
    }
    </table>
}
else
{
    <div style="color: Red"><b>No products found</b></div>
}
